<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class collect_model extends base_model {
	public function __construct() {
		parent::__construct();
	}
	
	//保存汇总记录的年月
	public function set_collect_month($collect_id) {
		$row = SQL::share('collect')->where($collect_id)->row('month, gmonth');
		if ($row) {
			if (strlen($row->month)) {
				$months = explode(',', str_replace('，', ',', $row->month));
				foreach ($months as $m) {
					if (SQL::share('collect_month')->where("collect_id='{$collect_id}' AND month='{$m}' AND type=0")->exist()) continue;
					SQL::share('collect_month')->insert(array('collect_id'=>$collect_id, 'month'=>$m, 'type'=>0, 'add_time'=>time()));
				}
			}
			if (strlen($row->gmonth)) {
				$months = explode(',', str_replace('，', ',', $row->gmonth));
				foreach ($months as $m) {
					if (SQL::share('collect_month')->where("collect_id='{$collect_id}' AND month='{$m}' AND type=1")->exist()) continue;
					SQL::share('collect_month')->insert(array('collect_id'=>$collect_id, 'month'=>$m, 'type'=>1, 'add_time'=>time()));
				}
			}
		}
	}
	
	//获取指定时间范围的月份数组
	public function getMonthRange($start, $end) {
		if (!$start || !$end) return array();
		$end = date('Ym', strtotime($end));
		$range = array();
		$i = 0;
		do {
			$m = date('Ym', strtotime($start . ' + ' . $i . ' month'));
			$month = date('Y-n', strtotime($start . ' + ' . $i . ' month'));
			if (!in_array($month, $range)) $range[] = $month;
			$i++;
		} while ($m < $end);
		return $range;
	}
	
	//月份数组排序且过滤重复
	public function sortMonths($months) {
		if (!$months || !is_array($months) || !count($months)) return array();
		usort($months, function($a, $b){
			$am = date('Ym', strtotime($a));
			$bm = date('Ym', strtotime($b));
			if ($am==$bm) return 0;
			return ($am<$bm) ? -1 : 1;
		});
		$range = array();
		foreach ($months as $k=>$month) {
			if (!strlen($month)) continue;
			$month = date('Y-n', strtotime($month));
			if (!in_array($month, $range)) $range[] = $month;
		}
		return $range;
	}
	
	//是否连续购买
	public function constant_buy($city_id, $idcard, $month) {
		$row = SQL::share('order o')->left('collect c', 'c.idcard=o.idcard')
			->where("c.city_id='{$city_id}' AND c.idcard='{$idcard}' AND o.status=1")->sort('o.id DESC')->row('c.month');
		if ($row) {
			$months = explode(',', str_replace('，', ',', $row->month));
			$buy_month = date('Y-m', strtotime($month));
			$last_month = date('Y-m', strtotime($months[count($months)-1]));
			$days = get_date_diff($buy_month, $last_month);
			$m = $days['month'];
			if ($m<=1) return true;
		}
		return false;
	}

	public function n_status($status) {
        $str = '';
        switch($status){
            case 0:$str = '需增员';break;
            case 1:$str = '需减员';break;
            case 2:$str = '已增员';break;
            case 3:$str = '已减员';break;
            case 4:$str = '增员失败';break;
            case 5:$str = '无';break;
        }
        return $str;
    }

    public function n_kstatus($status) {
        $str = '';
        switch($status){
            case 0:$str = '未扣费';break;
            case 1:$str = '已扣费';break;
            case 2:$str = '扣费失败';break;
            case 3:$str = '已退款';break;
        }
        return $str;
    }

    public function n_gstatus($status) {
        $str = '';
        switch($status){
            case 0:$str = '需增员';break;
            case 1:$str = '需减员';break;
            case 2:$str = '已增员';break;
            case 3:$str = '已减员';break;
            case 4:$str = '增员失败';break;
            case 5:$str = '无';break;
        }
        return $str;
    }

    public function n_gkstatus($status) {
        $str = '';
        switch($status){
            case 0:$str = '未扣费';break;
            case 1:$str = '已扣费';break;
            case 2:$str = '扣费失败';break;
            case 3:$str = '已退款';break;
        }
        return $str;
    }

	//社保增员状态
	public function status() {
		return array('需增员', '需减员', '已增员', '已减员', '增员失败', '无');
	}
	
	//社保扣费状态
	public function kstatus() {
		return array('未扣费', '已扣费', '扣费失败', '已退款');
	}
	
	//公积金增员状态
	public function gstatus() {
		return array('需增员', '需减员', '已增员', '已减员', '增员失败', '无');
	}
	
	//公积金扣费状态
	public function gkstatus() {
		return array('未扣费', '已扣费', '扣费失败', '已退款');
	}
	
	public function status_name($status) {
		$name = $status;
		$status = intval($status);
		$arr = $this->status();
		if ($status<0 || $status>=count($arr)) return $name;
		return $arr[$status];
	}
	
	public function kstatus_name($status) {
		$name = $status;
		$status = intval($status);
		$arr = $this->kstatus();
		if ($status<0 || $status>=count($arr)) return $name;
		return $arr[$status];
	}
	
	public function gstatus_name($status) {
		$name = $status;
		$status = intval($status);
		$arr = $this->gstatus();
		if ($status<0 || $status>=count($arr)) return $name;
		return $arr[$status];
	}
	
	public function gkstatus_name($status) {
		$name = $status;
		$status = intval($status);
		$arr = $this->gkstatus();
		if ($status<0 || $status>=count($arr)) return $name;
		return $arr[$status];
	}
	
	public function get_city() {
		$rs = SQL::share('service_city sc')->left('city c', 'sc.city_id=c.city_id')->sort('sc.id ASC')->find('sc.city_id, c.name as city_name');
		return $rs;
	}
}
